<?php

/**
 * 控制器抽象父类
 *
 * @author JiangJian <silverd@sohu.com>
 * $Id: Abstract.php 172 2012-11-20 03:37:20Z silverd30@gmail.com $
 */

abstract class Core_Controller_Abstract
{
    final public function __construct()
    {
        if (method_exists($this, 'init')) {
            $this->init();
        }
    }

    /**
     * 析构函数
     */
    final public function __destruct()
    {
    }
    
    public function init()
    {
    }

    /**
     * 动态获取变量
     *
     * @param string $key
     * @return mixed
     */
    public function __get($var)
    {
        switch ($var) {

            case '_request':
                return $this->_request  = $this->getRequest();

            case '_response':
                return $this->_response = $this->getResponse();

            case '_view' :
                return $this->_view     = $this->getView();

            case '_session':
                return $this->_session  = Com_Session::getInstance();

            case '_cookie':
                return $this->_cookie   = Com_Cookie::getInstance();

            case '_memcache' :
                return $this->_memcache = Com_Cache::getInstance('Memcache');

            case '_redis':
                return $this->_redis    = Com_Cache::getInstance('Redis');

            case '_lock':
                return $this->_lock     = new Com_Lock($this->_memcache);

            default:
                throw new Core_Exception_Fatal('Undefined controller property: ' . get_class($this) . '::' . $var);
        }
    }

    public function getRequest()
    {
        return Core_Request_Http::getInstance();
    }

    public function getResponse()
    {
        return Core_Response_Http::getInstance();
    }

    public function getView()
    {
        return Core_View::getInstance();
    }

    public function get($key)
    {
        return $this->_request->get($key);
    }

    public function getx($key)
    {
        return strip_tags(trim($this->_request->get($key)));
    }

    public function getInt($key)
    {
        return intval($this->_request->get($key));
    }

    public function isGet()
    {
        return $this->_request->isGet();
    }

    public function isPost()
    {
        return $this->_request->isPost();
    }

    public function isAjax()
    {
        return $this->_request->isXmlHttpRequest();
    }

    public function getQuery($key = null, $default = null)
    {
        return $this->_request->getQuery($key, $default);
    }

    public function getPost($key = null, $default = null)
    {
        return $this->_request->getPost($key, $default);
    }

    public function getPostx($key = null, $default = null)
    {
        return Helper_String::deepFilterDatas($this->_getPost($key, $default), array('strip_tags', 'trim'));
    }

    public function getParam($key = null, $default = null)
    {
        return $this->_request->getParam($key, $default);
    }

    public function getParams()
    {
        return $this->_request->getParams();
    }

    public function forward($controller, $action = 'index', $params = array())
    {
        $dispatchInfo = array(
            'controller' => $controller,
            'action'     => $action,
            'params'     => $params,
        );
        Core_App::getInstance()->setDispatchInfo($dispatchInfo)->setDispatched(false);
    }
}